﻿package com.wandian.util.loader
{
	import com.wandian.data.GameData;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;

    public class SWFLoader extends EventDispatcher
    {
        private var m_loadinfo:LoaderInfo;

        public function SWFLoader()
        {
            return;
        }

        public function get loadinfo() : LoaderInfo
        {
            return this.m_loadinfo;
        }

        public function Load(url:String) : void
        {
            var loader:* = new Loader();
            var context:LoaderContext = new LoaderContext();
//			context.securityDomain = SecurityDomain.currentDomain;
			context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
            trace(url);
            this.InitLoadEvent(loader.contentLoaderInfo);
			loader.load(new URLRequest(url), context);
            return;
        }

        public function GetClass(param1:String, param2:LoaderInfo = null) : Class
        {
            var name:* = param1;
            var info:* = param2;
            try
            {
                if (info == null)
                {
                    return ApplicationDomain.currentDomain.getDefinition(name) as Class;
                }
                return info.applicationDomain.getDefinition(name) as Class;
            }
            catch (e:ReferenceError)
            {
                return null;
            }
            return null;
        }

        private function InitLoadEvent(param1:LoaderInfo) : void
        {
            param1.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
            param1.addEventListener(Event.COMPLETE, this.onComplete);
            param1.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
            param1.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
            return;
        }

        private function RemoveLoadEvent(param1:LoaderInfo) : void
        {
            param1.removeEventListener(Event.COMPLETE, this.onComplete);
            param1.removeEventListener(ProgressEvent.PROGRESS, this.onProgress);
            param1.removeEventListener(IOErrorEvent.IO_ERROR, this.onError);
            param1.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
            return;
        }

        private function onComplete(event:Event) : void
        {
            var _loc_2:* = event.currentTarget as LoaderInfo;
            this.RemoveLoadEvent(_loc_2);
            this.m_loadinfo = _loc_2;
            this.dispatchEvent(event);
            return;
        }

        private function onProgress(event:ProgressEvent) : void
        {
            this.dispatchEvent(event);
            return;
        }

        private function onError(event:Event) : void
        {
            this.dispatchEvent(event);
            return;
        }

    }
}
